package com.donglan.pethome.pet.controller;

import com.alibaba.fastjson.JSONObject;
import com.donglan.pethome.basic.util.AjaxResult;
import com.donglan.pethome.basic.util.PageList;
import com.donglan.pethome.basic.util.RedisUtil;
import com.donglan.pethome.organization.domain.Employee;
import com.donglan.pethome.organization.domain.Shop;
import com.donglan.pethome.organization.service.ShopService;
import com.donglan.pethome.pet.domain.Pet;
import com.donglan.pethome.pet.domain.SearchMasterMessage;
import com.donglan.pethome.pet.query.SearchMasterMessageQuery;
import com.donglan.pethome.pet.service.PetService;
import com.donglan.pethome.pet.service.SearchMasterMsgService;
import com.donglan.pethome.pet.util.TemporaryAsShopLogin;
import com.donglan.pethome.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @Author: zhangdonghui
 * @Date: 2022/2/14 16:17
 * 寻主信息controller
 */
@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {

    @Autowired
    private SearchMasterMsgService seachMasterMsgService;
    @Autowired
    private ShopService shopService;
    @Autowired
    private PetService petService;

    /*
     * 功能描述 发布寻主信息
     * @author zhangdonghui
     * @date 2022/2/14 16:19
     * @param searchMasterMsg
     * @param request
     * @return AjaxResult
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMessage searchMasterMessage, HttpServletRequest request){
        try {
            //获取token值
            String token = request.getHeader("U-TOKEN");
            //获取登录信息对象
            String loginInfo = RedisUtil.INSTANCE.get(token);
            // 转成User对象
            User user = JSONObject.parseObject(loginInfo, User.class);
            // 设置登录用户/设置发布人
            searchMasterMessage.setUser(user);
            //发布寻主信息，信息对象和登录信息对象
            seachMasterMsgService.publish(searchMasterMessage);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }

    /*
     * 功能描述 获取待处理寻主信息
     * @author zhangdonghui
     * @date 2022/2/15 18:03
     * @param searchMasterMsgQuery
     * @param request
     * @return PageInfo<SearchMasterMsg>
     */
    @PatchMapping("/pendingMessage")
    public PageList<SearchMasterMessage> pendingMessage(
            @RequestBody SearchMasterMessageQuery searchMasterMsgQuery){
        // 获取到后台的当前登录的员工Employee
        Employee loginEmployee = TemporaryAsShopLogin.getLoginUser();
        // 通过店铺业务对象通过员工id获取店铺对象
        Shop shop = shopService.loadShopByEmployeeId(loginEmployee.getId());
        // 设置店铺id
        searchMasterMsgQuery.setShopId(shop.getId());
        // 查询待处理的寻主信息
        searchMasterMsgQuery.setState(0);
        return seachMasterMsgService.loadPageByQuery(searchMasterMsgQuery);
    }


    /*
     * 功能描述 处理寻主信息对象
     * @author zhangdonghui
     * @date 2022/2/15 23:34
     * @param pet
     * @param request
     * @return com.donglan.pethome.basic.util.AjaxResult
     */
    @PutMapping("/handle")
    public AjaxResult handle(@RequestBody Pet pet){
        try {
            // 设置下架时间
            pet.setOffsaletime(new Date());
            // 获取到后台的当前登录的员工Employee
            Employee loginEmployee = TemporaryAsShopLogin.getLoginUser();
            // 通过店铺业务对象通过员工id获取店铺对象
            Shop shop = shopService.loadShopByEmployeeId(loginEmployee.getId());
            // 设置店铺
            pet.setShop(shop);
            // 添加宠物
            petService.add(pet);
            // 添加收购订单


            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }


}
